// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
})

const db = cloud.database();

// 创建集合云函数入口函数
exports.main = async (event, context) => {
    try {
        // 请求参数
        let {
            conditions
        } = event.data

        if (typeof conditions == "string") {
            // 未传递筛选条件时 conditions会默认等于"1"
            conditions = {}
        }

        let query = db.collection('goodsList')

        query = query.where(conditions) // 添加筛选条件
        query = query.orderBy('createTime', 'desc') // 排序

        const data = await query.get()
        return {
            code: 200,
            msg: "操作成功",
            data: data.data,
        }
    } catch (err) {
        return {
            code: 500,
            message: err.message
        }
    }
}